export default {
	data() {
		return {
			activeStyle: {
				width: '.5rem',
				transform: 'translateX(0rem)'
			},
			memberId: 0,
			searchText: '',
			type: 'card',
			memberCoupon: [],
			startDate: '1900-1-1',
			sex: [{
					text: '未知',
					value: 0
				},
				{
					text: '男',
					value: 1
				}, {
					text: '女',
					value: 2
				},
			],
			memberData: {
				sex: 0,
				mobile: '',
				nickname: '',
				birthday: '',
				headimg: '',
				member_level: 0,
				member_level_name: '',
			},
			flag: false,
			memberCardData: {
				page: 0,
				total: 1,
				list: [],
				index: 0,
				currData: {},
				selected: {}
			},
			itemNum: 1,
			memberLevelList: [],
			active: '' // 活动值，记录当前进行的操作
		}
	},
	mounted() {
		this.mediaQueryOb = uni.createMediaQueryObserver(this)

		this.mediaQueryOb.observe({
			maxWidth: 1500
		}, matches => {
			if (matches) this.itemNum = 1;
		})

		this.mediaQueryOb.observe({
			minWidth: 1501,
			maxWidth: 1900,
		}, matches => {
			if (matches) this.itemNum = 2;
		})

		this.mediaQueryOb.observe({
			minWidth: 1901,
		}, matches => {
			if (matches) this.itemNum = 3;
		})
	},
	created() {
		this.getMemberLevel();
	},
	methods: {
		selectMemberLevel(index, item) {

			if (index >= 0) {
				this.memberData.member_level = item.value;
				this.memberData.member_level_name = item.label;
			} else {
				this.memberData.member_level = '';
				this.memberData.member_level_name = '';
			}
			this.$forceUpdate();

		},
		// 添加图片
		addImg(i) {
			this.$util.upload(1, {
				path: 'headimg'
			}, res => {
				if (res.length > 0) {
					this.memberData.headimg = res[0];
					this.$forceUpdate();
				}
			});
		},
		getMemberLevel() {
			this.memberLevelList = [];
			this.$api.sendRequest({
				url: '/cashier/storeapi/memberlevel/lists',
				success: res => {
					if (res.code == 0 && res.data) {
						for (let i in res.data) {
							this.memberLevelList.push({
								'label': res.data[i]['level_name'],
								'value': res.data[i]['level_id'].toString(),
								'disabled': false
							});
						}
					}
				}
			});
		},
		addSwitch() {
			this.type = 'addmember';
			this.memberData = {
				sex: 0,
				mobile: this.searchText,
				nickname: '',
				birthday: '',
				headimg: '',
				member_level: '',
				member_level_name: '',
			}
		},
		addMember() {
			if (this.verify()) {
				if (this.flag) return;
				this.flag = true;
				let data = this.memberData;
				this.$api.sendRequest({
					url: '/cashier/storeapi/member/addMember',
					data: this.memberData,
					success: res => {
						if (res.code == 0 && res.data) {
							this.memberId = res.data;
							this.type = 'member';
							this.getMemberInfo();
						} else {
							this.$util.showToast({
								title: res.message
							})
						}
						this.flag = false;
					}
				});
			}
		},
		verify() {
			if (!this.memberData.mobile) {
				this.$util.showToast({
					title: '请输入会员手机号'
				})
				return false;
			}
			if (!this.$util.verifyMobile(this.memberData.mobile)) {
				this.$util.showToast({
					title: '请输入正确的手机号码'
				})
				return false;
			}
			return true;
		},
		changeTime(e) {
			this.memberData.birthday = e;
		},
		recharge() {
			this.$util.redirectTo('/pages/recharge/index');
		},
		coupon() {
			this.type = 'coupon';
			this.memberCoupon = [];
			this.$api.sendRequest({
				url: '/cashier/storeapi/member/coupon',
				data: {
					member_id: this.info.member_id,
					page: 1,
					page_size: 0,
					state: 1
				},
				success: res => {
					if (res.code == 0 && res.data) {
						this.memberCoupon = res.data.list;
					}
				}
			});
		},
		searchMember() {
			if (!/[\S]+/.test(this.searchText)) {
				this.$util.showToast({
					title: '请输入搜索内容'
				})
				return;
			}
			this.$api.sendRequest({
				url: '/cashier/storeapi/member/searchmember',
				data: {
					search_text: this.searchText
				},
				success: res => {
					if (res.code == 0 && res.data) {
						this.memberId = res.data.member_id;
						this.searchText = '';
						this.getMemberInfo();
					} else {
						this.$util.showToast({
							title: '没有查询到对应会员'
						})
					}
				}
			});
		},
		searchMemberByType(searchText, searchType) {
			this.$api.sendRequest({
				url: '/cashier/storeapi/member/searchmember',
				data: {
					search_text: searchText,
					search_type: searchType
				},
				success: res => {
					if (res.code == 0 && res.data) {
						this.memberId = res.data.member_id;
						this.searchText = '';
						this.getMemberInfo();
					} else {
						this.$util.showToast({
							title: '没有查询到对应会员'
						})
					}
				}
			});
		},
		getMemberInfo() {
			this.$api.sendRequest({
				url: '/cashier/storeapi/member/info',
				data: {
					member_id: this.memberId
				},
				success: res => {
					if (res.code == 0 && res.data) {
						this.$store.commit('setMemberInfo', res.data);
					} else {
						this.$util.showToast({
							title: '未获取到会员信息'
						})
					}
				}
			})
		},
		/**
		 * 获取会员项目
		 */
		getMemberCard() {
			if (this.memberCardData.page + 1 > this.memberCardData.total) return;
			this.memberCardData.page += 1;

			this.$api.sendRequest({
				url: '/cardservice/storeapi/membercard/lists',
				data: {
					status: 1,
					page: this.memberCardData.page,
					member_id: this.info.member_id
				},
				success: res => {
					if (res.code == 0) {
						this.memberCardData.total = res.data.page_count || 1;
						if (this.memberCardData.page == 1) {
							this.memberCardData.list = [];
							if (res.data.count) this.selectMemberCard(res.data.list[0], 0);
						}
						if (res.data.list.length) this.memberCardData.list = this.memberCardData.list
							.concat(res.data.list);
					}
				}
			})
		},
		/**
		 * 选择会员套餐
		 * @param {Object} data
		 * @param {Object} index
		 */
		selectMemberCard(data, index) {
			this.memberCardData.index = index;
			this.memberCardData.currData = this.$util.deepClone(data);
			this.memberCardData.selected = {};
		},
		/**
		 * 选择会员套餐商品项
		 * @param {Object} data
		 * @param {Object} index
		 */
		selectMemberCardItem(data, index) {
			if (this.memberCardData.selected['item_' + data.item_id]) {
				if (data.card_type == 'commoncard') {
					this.memberCardData.currData.total_use_num -= this.memberCardData.selected['item_' + data.item_id]
						.input_num;
				}
				delete this.memberCardData.selected['item_' + data.item_id];
			} else {
				if (!this.checkStatus(data)) return;
				this.memberCardData.selected['item_' + data.item_id] = this.$util.deepClone(data);
				this.memberCardData.selected['item_' + data.item_id].input_num = 1;
				this.memberCardData.selected['item_' + data.item_id].index = index;
				this.memberCardData.selected['item_' + data.item_id].card_name = this.memberCardData.currData
					.goods_name;
				if (data.card_type == 'commoncard') {
					this.memberCardData.currData.total_use_num += 1;
				}
			}
			this.active = 'selectMemberCardItem';
			this.$forceUpdate();
		},
		/**
		 * 加入购物车
		 */
		selectGoods() {
			this.active = '';
			if (!Object.keys(this.memberCardData.selected).length) {
				this.$util.showToast({
					title: '请选择服务/商品',
				})
				return;
			}

			let arr = [];
			Object.keys(this.memberCardData.selected).forEach((key) => {
				let data = this.memberCardData.selected[key];
				data.card_index = this.memberCardData.index;
				data.num = data.input_num;
				arr.push(this.$util.deepClone(data));
				this.memberCardData.list[this.memberCardData.index].total_use_num += data.input_num;
				this.memberCardData.list[this.memberCardData.index].item_list[data.index].use_num += data
					.input_num;
				this.memberCardData.currData.item_list[data.index].use_num += data.input_num;
			})
			this.$emit('selectGoods', arr);
			this.memberCardData.selected = {};
		},
		/**
		 * 数量减
		 * @param {Object} data
		 */
		itemDec(data) {
			let currData = this.memberCardData.currData;
			if (this.memberCardData.selected['item_' + data.item_id].input_num > 1) {
				this.memberCardData.selected['item_' + data.item_id].input_num -= 1;
				if (data.card_type == 'commoncard') {
					currData.total_use_num -= 1;
				}
				this.$forceUpdate();
			}
		},
		/**
		 * 数量加
		 * @param {Object} data
		 */
		itemInc(data) {
			let currData = this.memberCardData.currData;
			if (data.card_type == 'commoncard') {
				if ((currData.total_num - currData.total_use_num - 1) < 0) return;
			} else if (data.card_type != 'timecard') {
				if ((data.num - data.use_num - this.memberCardData.selected['item_' + data.item_id].input_num - 1) < 0)
					return;
			}
			if (data.card_type == 'commoncard') {
				currData.total_use_num += 1;
			}
			this.memberCardData.selected['item_' + data.item_id].input_num += 1;
			this.$forceUpdate();
		},
		checkStatus(data) {
			let currData = this.$util.deepClone(this.memberCardData.currData);
			if (data.card_type == 'commoncard') {
				return currData.total_num > currData.total_use_num;
			} else if (data.card_type != 'timecard') {
				return data.num > data.use_num;
			}
			return true;
		}
	}
}